package com.common.core.utils;

import android.content.Context;
import android.content.res.AssetManager;

import com.common.http.basecore.utils.LogInfo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;

/**
 * Created by lee on 16-4-1.
 */
public class FileUtils {

    /**
     *  删除文件夹及其子文件
     * @param file
     */

    public static void RecursionDeleteFile(File file){
        if(!file.exists()){
            return;
        }
        if(file.isFile()){
            file.delete();
            return;
        }
        if(file.isDirectory()){
            File[] childFile = file.listFiles();
            if(childFile == null || childFile.length == 0){
                file.delete();
                return;
            }
            for(File f : childFile){
                RecursionDeleteFile(f);
            }
            file.delete();
        }
    }


    public static String fileToType(File file){
        String path = file.getAbsolutePath();
        String type = null;
        if(path.lastIndexOf('.') != -1){
            type = path.substring(path.lastIndexOf('.'), path.length());
        }
        if(null == type){
            return "";
        }else{
            return type.replace(".", "").toLowerCase();
        }
    }

    public static String fileToType(String fileName){
        String type = null;
        if(fileName.lastIndexOf('.') != -1){
            type = fileName.substring(fileName.lastIndexOf('.'), fileName.length());
        }
        if(null == type){
            return "";
        }else{
            return type.replace(".", "").toLowerCase();
        }
    }
    /**
     * 将fileName转换成VideoTitle
     */
    public static String pathToTitle(File file) {
        if (null != file) {
            return pathToTitle(file.getName());
        } else {
            return "";
        }
    }
    /**
     * 将fileName转换成VideoTitle
     */
    public static String pathToTitle(String fileName) {
        String tmp = null;
        if (null != fileName && !"".equals(fileName)) {
            int index = fileName.lastIndexOf(".");
            if(index>0){
                tmp = fileName.substring(0, index);
            }else{
                tmp = fileName;
            }
        } else {
            tmp = "";
        }
        LogInfo.log("pathToTitle", "pathToTitle = " + tmp);
        return tmp;
    }

    /**
     * 从文件路径截取文件名
     * */
    public static String getFileName(String pathandname){
        LogInfo.log("king", "filePath = " + pathandname);
        int start=pathandname.lastIndexOf("/");
        int end=pathandname.lastIndexOf(".");
        if(start!=-1 && end!=-1){
            return pathandname.substring(start+1,end);
        }else{
            return null;
        }
    }

    /**
     * 获取文件大小
     * */
    public static String getFileSize(String filePath) {
        FileChannel fc= null;
        try {
            File f= new File(filePath);
            if (f.exists() && f.isFile()){
                FileInputStream fis= new FileInputStream(f);
                fc= fis.getChannel();
                return decodeBit(fc.size());
            }else{
                LogInfo.log("file doesn't exist or is not a file");
            }
        } catch (FileNotFoundException e) {
            LogInfo.log(e.toString());
        } catch (IOException e) {
            LogInfo.log(e.toString());
        } finally {
            if (null!=fc){
                try{
                    fc.close();
                }catch(IOException e){
                    LogInfo.log(e.toString());
                }
            }
        }
        return "0";
    }

    /**
     * 转换字节
     * */
    public static String decodeBit(long size){
        long B = size / 8;
        if(B<1024){
            return B+"B";
        }else{
            long KB = B / 1024;
            if(KB <1024){
                return KB+"KB";
            }else{
                long M = KB / 1024;
                if(M<1024){
                    return M+"M";
                }else {
                    return "1G";
                }
            }
        }
    }

    public static String readStringsContent(Context context, String filePath) {
        String content = null;
        InputStream is = null;
        AssetManager assetManager = context.getAssets();
        try {
            is = assetManager.open(filePath);
            if (is != null) {
                byte[] buffer = new byte[is.available()];
                is.read(buffer);
                content= new String(buffer, "utf-8");
            }
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return content;
    }

}
